package com.northchurchcolumbus.podcast.ncpodcaster;

import java.net.URL;
import java.util.Calendar;
import java.util.Map;

import com.apple.eawt.ApplicationAdapter;
import com.google.gdata.client.GoogleService;
import com.google.gdata.data.DateTime;
import com.google.gdata.data.Entry;
import com.google.gdata.data.HtmlTextConstruct;
import com.google.gdata.data.PlainTextConstruct;

public class BloggerPodPoster implements PodPoster
{

    public void post(String title, String author, Calendar pubDate, URL podcastUrl,
            Map<String, String> ctx, boolean isDraft) throws Exception
    {
        GoogleService myService = new GoogleService("blogger", ctx.get("blogger_appid"));
        myService.setUserCredentials(ctx.get("blog_username"), ctx.get("blog_password"));
                
        StringBuilder content = new StringBuilder(author);
        content.append("<br />");
        content.append("<object type=\"application/x-shockwave-flash\" data=\"http://dl.dropbox.com/u/11089098/podcast/mp3player/dewplayer.swf\" width=\"200\" height=\"20\">      <param name=\"movie\" value=\"http://dl.dropbox.com/u/11089098/podcast/mp3player/dewplayer.swf\" /><param name=\"wmode\" value=\"transparent\" /><param name=\"movie\" value=\"dewplayer-bubble.swf\" />");
        content.append("<param name=\"flashvars\" value=\"mp3=");
        content.append(podcastUrl.toExternalForm());
        content.append("&amp;showtime=1\" /></object>");
        // Create the entry to insert
        Entry myEntry = new Entry();
        myEntry.setTitle(new PlainTextConstruct(title));
        myEntry.setContent(new HtmlTextConstruct(content.toString()));
        myEntry.setPublished(new DateTime(pubDate.getTimeInMillis(), 0));
        myEntry.addLink("enclosure", "audio/mpeg", podcastUrl.toExternalForm());
        myEntry.setDraft(isDraft);
        // Ask the service to insert the new entry
        URL postUrl = new URL("http://www.blogger.com/feeds/" + ctx.get("blog_id") + "/posts/default");
        myService.insert(postUrl, myEntry);
    }

}
